package com.zhongshengnetwork.rightbe.keyboard.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zhongshengnetwork.rightbe.CustomApplication;
import com.zhongshengnetwork.rightbe.R;
import com.zhongshengnetwork.rightbe.common.CircleTransform;
import com.zhongshengnetwork.rightbe.keyboard.SimpleCommonUtils;
import com.zhongshengnetwork.rightbe.keyboard.data.ImMsgBean;
import com.zhongshengnetwork.rightbe.keyboard.utils.ImageLoadUtils;
import com.zhongshengnetwork.rightbe.dbmodel.LetterdbModel;
import com.zhongshengnetwork.rightbe.Constant.LetterConstant;
import com.zhongshengnetwork.rightbe.letter.holder.TextMsgViewHolder;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import sj.keyboard.utils.imageloader.ImageBase;

public class ChattingListAdapter extends BaseAdapter{// implements View.OnClickListener, View.OnLongClickListener

    private Activity mActivity;
    private LayoutInflater mInflater;
    private List<LetterdbModel> mData;

    public ChattingListAdapter(Activity activity) {
        this.mActivity = activity;
        mInflater = LayoutInflater.from(activity);
    }

    public void addData(LetterdbModel chatData){
        if (chatData == null){
            return;
        }
        if (mData == null) {
            mData = new ArrayList<>();
        }
        mData.add(chatData);
        this.notifyDataSetChanged();
    }

    public void addDataList(List<LetterdbModel> data) {
        if (data == null || data.size() == 0) {
            return;
        }
        if (mData == null) {
            mData = new ArrayList<>();
        }
        Collections.reverse(data);   //这行就是将list的内容反转，下面再装进adapter里，就可以倒序显示了
        for (LetterdbModel chatData : data) {
            mData.add(chatData);
        }
        this.notifyDataSetChanged();
    }

    public void updateData(int position, LetterdbModel chatData){
        if (mData == null || mData.size() <= position){
            return;
        }
        mData.set(position,chatData);
        this.notifyDataSetChanged();
    }

    public int getCurrentCount(){
        if (mData == null){
            mData = new ArrayList<>();
            return 0;
        }
        return mData.size();
    }

    @Override
    public int getCount() {
        return mData == null ? 0 : mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final LetterdbModel chatData = mData.get(position);
        View holderView = null;
        switch (chatData.getType()) {
            case LetterConstant.sx_textMsg://文本消息
                TextMsgViewHolder holder;
                if (convertView == null) {
                    holder = new TextMsgViewHolder();
                    holderView = mInflater.inflate(R.layout.text_msg_view, null);
                    holderView.setFocusable(true);
                    holder.right_avatar = (ImageView) holderView.findViewById(R.id.text_right_avatar);
                    holder.right_content = (TextView) holderView.findViewById(R.id.text_right_content);
                    holder.right_state = (ImageView) holderView.findViewById(R.id.state_text);
                    holder.left_avatar = (ImageView) holderView.findViewById(R.id.text_left_avatar);
                    holder.left_content = (TextView) holderView.findViewById(R.id.text_left_content);
                    holder.name = (TextView) holderView.findViewById(R.id.text_name);
                    holderView.setTag(holder);
                    convertView = holderView;
                } else {
                    holder = (TextMsgViewHolder) convertView.getTag();
                }

                if (chatData.getIsSelf() == 1){//自己发送的消息
                    holder.left_avatar.setVisibility(View.INVISIBLE);
                    holder.left_content.setVisibility(View.INVISIBLE);
                    holder.name.setVisibility(View.INVISIBLE);
                    holder.right_avatar.setVisibility(View.VISIBLE);
                    holder.right_content.setVisibility(View.VISIBLE);
                    Glide.with(CustomApplication.mContext).load(chatData.getHeader()).transform(new CircleTransform(CustomApplication.mContext)).dontAnimate().into(holder.right_avatar);
                    if (chatData.getState() == LetterConstant.sx_normal_state){
                        holder.right_state.clearAnimation();
                        holder.right_state.setVisibility(View.INVISIBLE);
                    }else if(chatData.getState() == LetterConstant.sx_sending_state){
                        holder.right_state.setVisibility(View.VISIBLE);
                        holder.right_state.setImageResource(R.mipmap.sending);
                        Animation mAnimation = AnimationUtils.loadAnimation(this.mActivity, R.anim.roraterepeat);
                        holder.right_state.startAnimation(mAnimation);
                    }else {
                        holder.right_state.clearAnimation();
                        holder.right_state.setVisibility(View.VISIBLE);
                        holder.right_state.setImageResource(R.mipmap.resent);
                    }


                }else {//接收到的消息
                    holder.left_avatar.setVisibility(View.VISIBLE);
                    holder.left_content.setVisibility(View.VISIBLE);
                    holder.name.setVisibility(View.VISIBLE);
                    holder.right_avatar.setVisibility(View.INVISIBLE);
                    holder.right_content.setVisibility(View.INVISIBLE);
                    holder.right_state.clearAnimation();
                    holder.right_state.setVisibility(View.GONE);
                    holder.name.setText(chatData.getNickname());
                    Glide.with(CustomApplication.mContext).load(chatData.getHeader()).transform(new CircleTransform(CustomApplication.mContext)).dontAnimate().into(holder.left_avatar);
                }
                /*
                holder.left_content.setTag(position);
                holder.left_content.setOnClickListener(this);
                holder.left_content.setOnLongClickListener(this);
                holder.right_content.setTag(position);
                holder.right_content.setOnClickListener(this);
                holder.right_content.setOnLongClickListener(this);
                */

                disPlayTextView(position,holderView,holder,chatData);
                break;
            /*
            case VIEW_TYPE_RIGHT_TEXT:

                break;
            case VIEW_TYPE_LEFT_IMAGE:
                ViewHolderLeftImage imageHolder;
                if (convertView == null) {
                    imageHolder = new ViewHolderLeftImage();
                    holderView = mInflater.inflate(R.layout.listitem_chat_left_image, null);
                    holderView.setFocusable(true);
                    imageHolder.iv_avatar = (ImageView) holderView.findViewById(R.id.iv_avatar);
                    imageHolder.iv_image = (ImageView) holderView.findViewById(R.id.iv_image);
                    holderView.setTag(imageHolder);
                    convertView = holderView;
                } else {
                    imageHolder = (ViewHolderLeftImage) convertView.getTag();
                }
                disPlayLeftImageView(position, convertView, imageHolder, bean);
                break;
            */
            default:
                convertView = new View(mActivity);
                break;
        }
        return convertView;
    }

    public void disPlayTextView(int position, View view, TextMsgViewHolder holder, LetterdbModel chatData) {
        if (chatData.getIsSelf() == 1){
            SimpleCommonUtils.spannableEmoticonFilter(holder.right_content, chatData.getContent());
        }else {
            SimpleCommonUtils.spannableEmoticonFilter(holder.left_content, chatData.getContent());
        }
    }

    public void disPlayLeftImageView(int position, View view, ViewHolderLeftImage holder, ImMsgBean bean) {
        try {
            if (ImageBase.Scheme.FILE == ImageBase.Scheme.ofUri(bean.getImage())) {
                String filePath = ImageBase.Scheme.FILE.crop(bean.getImage());
                Glide.with(holder.iv_image.getContext())
                        .load(filePath)
                        .dontAnimate().into(holder.iv_image);
            } else {
                ImageLoadUtils.getInstance(mActivity).displayImage(bean.getImage(), holder.iv_image);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public final class ViewHolderLeftImage {
        public ImageView iv_avatar;
        public ImageView iv_image;
    }

    //点击事件
    public void onClick(View view){
        switch (view.getId()){
            case R.id.text_left_content:
                System.out.print("点击了左边的文字\n");
                break;
            case R.id.text_right_content:
                System.out.print("点击了右边\n");
                break;
            default:

                break;
        }
    }


    public boolean onLongClick(View view){
        switch (view.getId()){
            case R.id.text_left_content:
                System.out.print("长安了左边\n");
                break;
            case R.id.text_right_content:
                System.out.print("长安了右边\n");
                break;
            default:

                break;
        }
        return true;
    }



}